﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Collections;
using System.Text;

namespace Catering.Catering_Page.system
{
    public partial class Role_Manage : System.Web.UI.Page
    {
        public DataTable dt_Module = new DataTable();
        public DataTable dt_ModuleInfo = new DataTable();

        public string m_ModuleList = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadTV();
            }
            if (Request.QueryString["Rid"] != null)
            {
                this.TV_Seleteed();
                this.Tv_Role_SelectedNodeChanged(null, null);
            }
            this.LoadData(0);
        }

        //加载页面数据
        public void LoadData(int parentid)
        {
            StringBuilder result = new StringBuilder("");
            DataTable dt = new Catering_Rules.Module().GetChildNodeListByParentID(0);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    result.Append("<div class='modulediv'>");
                    string moduleName = dt.Rows[i]["ModuleName"].ToString();//块名称
                    int moduleID = Convert.ToInt32(dt.Rows[i]["ID"]);
                    DataTable tempdt = new Catering_Rules.Module_operating().GetList("ModuleID=" + moduleID).Tables[0];
                    result.Append("<div class='modulet' cellspacing='5'><table><tr><td><span class='modulets'>" + moduleName + "</span></td>");
                    if (tempdt != null && tempdt.Rows.Count > 0)
                    {
                        for (int j = 0; j < tempdt.Rows.Count; j++)
                        {
                            string tagName = tempdt.Rows[j]["operating"].ToString();
                            int tempid = Convert.ToInt32(tempdt.Rows[j]["ID"]);
                            string checkstate = IsExistsGN(tempid.ToString()) ? "checked=\"checked\"" : "";
                            result.Append("<td width='20'>&nbsp;</td><td><input type='checkbox' Pid='ck_Module_" + moduleID + "' " + checkstate + " name='ck_gn' value='" + moduleID + "|" + tempid + "' />&nbsp;" + tagName + "</td>");
                        }
                    }
                    result.Append("</tr></table></div>");
                    DataTable tempr = new Catering_Rules.Module().GetChildNodeListByParentID(moduleID);
                    if (tempr != null && tempr.Rows.Count > 0)
                    {
                        for (int k = 0; k < tempr.Rows.Count; k++)
                        {
                            moduleName = tempr.Rows[k]["ModuleName"].ToString();//块名称
                            moduleID = Convert.ToInt32(tempr.Rows[k]["ID"]);
                            string checkstate = IsExistsModule(moduleID.ToString()) ? "checked=\"checked\"" : "";
                            result.Append("<div class='kufang'>");
                            result.Append("<h2 class='gn_title'>");
                            result.Append("<input type='checkbox' cid='ck_Module_" + moduleID + "' " + checkstate + " name='ck_Module'  value='" + moduleID + "' />&nbsp;" + moduleName + "</h2>");
                            result.Append("<div class='gn_list'>");
                            result.Append("<ul>");
                            tempdt = new Catering_Rules.Module_operating().GetList("ModuleID=" + moduleID).Tables[0];
                            if (tempdt != null && tempdt.Rows.Count > 0)
                            {
                                foreach (System.Data.DataRow rowinfo in tempdt.Rows)
                                {
                                    string tagName = rowinfo["operating"].ToString();
                                    int tempid = Convert.ToInt32(rowinfo["ID"]);
                                    checkstate = IsExistsGN(tempid.ToString()) ? "checked=\"checked\"" : "";
                                    result.Append("<li><input type='checkbox' Pid='ck_Module_" + moduleID + "'  " + checkstate + " name='ck_gn' value='" + moduleID + "|" + tempid + "' />" + tagName + "</li>");                                   
                                }
                            }
                            result.Append("</ul>");
                            result.Append("<div style='height:0px; margin:0px; padding:0px; clear:both; line-height:0px; overflow:hidden;'></div>");
                            result.Append("</div>");
                            result.Append("</div>");
                        }
                    }
                    tempdt.Dispose();
                    tempr.Dispose();
                    result.Append("</div>");
                }
                dt.Dispose();
            }
            m_ModuleList = result.ToString();
        }

        //加载对应功能
        public void LoadMDInfo(string uid)
        {
            this.dt_ModuleInfo = new Catering_Rules.Module_operating().GetList("ModuleID=" + uid).Tables[0];
        }

        //绑定节点
        public void LoadTV()
        {
            this.Tv_Role.Nodes.Clear();
            DataTable dt_Role = new Catering_Rules.Role().GetAllList().Tables[0];
            if (dt_Role != null && dt_Role.Rows.Count > 0)
            {
                foreach (DataRow row in dt_Role.Rows)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = row["RoleName"].ToString();
                    tn.Value = row["id"].ToString();
                    this.Tv_Role.Nodes.Add(tn);
                }
            }
        }

        //选中对应节点
        public void TV_Seleteed()
        {
            string uid = Request.QueryString["Rid"].ToString();
            foreach (TreeNode tn in this.Tv_Role.Nodes)
            {
                if (tn.Value == uid)
                {
                    tn.Selected = true;
                }
            }
        }

        //选择角色
        protected void Tv_Role_SelectedNodeChanged(object sender, EventArgs e)
        {
            Catering_Model.Role md = new Catering_Rules.Role().GetModel(Convert.ToInt32(this.Tv_Role.SelectedValue));
            this.txt_name.Text = md.RoleName;
            this.txt_back.Text = md.Back;
            this.txt_QuanZhong.Text = md.temp1.ToString();
            this.HidRid.Value = this.Tv_Role.SelectedValue;
            this.LoadData(0);
        }

        //保存角色
        [WebMethod]
        public static ArrayList btn_Save(string Rid, string idlist)
        {
            new Catering_Rules.Role_Module().DeleteListBywhere("RoleID=" + Rid);
            ArrayList al = new ArrayList();
            string[] s = idlist.TrimEnd(',').Split(',');
            foreach (string strid in s)
            {
                Catering_Model.Role_Module md = new Catering_Model.Role_Module();
                md.id = new Catering_Rules.Role_Module().GetMaxId();
                if (strid.IndexOf('|') < 0)
                {
                    md.ModuleID = Convert.ToInt32(strid);
                    md.OperatingID = -1;
                }
                else
                {
                    md.ModuleID = Convert.ToInt32(strid.Split('|')[0]);
                    md.OperatingID = Convert.ToInt32(strid.Split('|')[1]);
                }
                md.RoleID = Convert.ToInt32(Rid);
                new Catering_Rules.Role_Module().Add(md);
            }
            al.Add("OK");
            al.Add(Rid);
            return al;
        }

        //添加角色
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (this.txt_name.Text.Trim() == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色名称不能为空!');</script>");
                return;
            }
            int quanzhong = 1;
            Int32.TryParse(this.txt_QuanZhong.Text.Trim(), out quanzhong);
            Catering_Model.Role md = new Catering_Model.Role();
            md.id = new Catering_Rules.Role().GetMaxId();
            md.RoleName = this.txt_name.Text.Trim();
            md.Back = this.txt_back.Text.Trim();
            md.temp1 = quanzhong;
            if (new Catering_Rules.Role().Exists(md.RoleName, 0))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此角色已存在！');</script>");
            }
            else
            {
                new Catering_Rules.Role().Add(md);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功');</script>");
            }
            this.LoadTV();
        }

        //修改角色
        protected void btn_Edit_Click(object sender, EventArgs e)
        {
            if (this.Tv_Role.SelectedNode == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择角色节点');</script>");
                return;
            }
            int quanzhong = 1;
            Int32.TryParse(this.txt_QuanZhong.Text.Trim(), out quanzhong);
            Catering_Model.Role md = new Catering_Model.Role();
            md.id = Convert.ToInt32(this.Tv_Role.SelectedValue);
            md.RoleName = this.txt_name.Text.Trim();
            md.Back = this.txt_back.Text.Trim();
            md.temp1 = quanzhong;
            if (new Catering_Rules.Role().Exists(md.RoleName, md.id))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此角色已存在！');</script>");
            }
            else
            {
                new Catering_Rules.Role().Update(md);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功');</script>");
            }
            this.LoadTV();
        }

        //判断是否有该模块
        public bool IsExistsModule(string uid)
        {
            if (this.Tv_Role.SelectedNode != null)
            {
                DataTable dt_Role_Module = new Catering_Rules.Role_Module().GetList("ModuleID=" + uid + " and RoleID=" + this.Tv_Role.SelectedValue).Tables[0];
                return (dt_Role_Module != null && dt_Role_Module.Rows.Count > 0);
            }
            else
            {
                return false;
            }
        }

        //判断是否有该功能
        public bool IsExistsGN(string uid)
        {
            if (this.Tv_Role.SelectedNode != null)
            {
                DataTable dt_Role_Module = new Catering_Rules.Role_Module().GetList("OperatingID=" + uid + " and RoleID=" + this.Tv_Role.SelectedValue).Tables[0];
                return (dt_Role_Module != null && dt_Role_Module.Rows.Count > 0);
            }
            else
            {
                return false;
            }
        }

    }
}